package com;

import com.solo_project.Coach;
import com.solo_project.League;
import com.solo_project.Player;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

/**
 * Created by ThoughtWorker on 1/16/14.
 */
public class LeagueTest {
    private String name1 = "Dan Park" ;
    private String option1 = "List Players";
    private String line = "****************************";

    private League league;
    private PrintStream printStream;
    private BufferedReader reader;


    @Before
    public void setUp() throws Exception {
        reader = mock(BufferedReader.class);
        printStream = mock(PrintStream.class);
        List<Player> playerList = new ArrayList<Player>();
        List<Coach> coachList = new ArrayList<Coach>();
        league = new League(printStream, reader, playerList, coachList);
    }

    @Test
    public void ShouldListNamesOfPlayersInLeague(){
        league.addPlayer(name1);
        league.listPlayers();
        verify(printStream).println(name1);
    }

    @Test
    public void shouldShowOptionsForMenu() {
        league.displayOptions();
        verify(printStream).println(option1);
    }

    @Test
    public void shouldReturnOneWhenUserInputIsOne() throws IOException {
        when(reader.readLine())
                .thenReturn("1")
                .thenReturn(null);
        String userChoice = league.receiveInput();
        assertThat(userChoice, is("1"));
    }

    @Test
    public void shouldListPlayersWhenInputIsOne() throws IOException {
        league.addPlayer(name1);
        when(reader.readLine())
                .thenReturn("1")
                .thenReturn(null);
        league.menu();
        verify(printStream).println("Players:");
     }

    @Test
    public void shouldFindPlayerWhenValidNameInput() throws IOException {
        league.addPlayer(name1, "ThoughtWorkers", 1, 27);
        when(reader.readLine())
                .thenReturn(name1)
                .thenReturn("1");
        league.findPlayer();
        verify(printStream).println(name1);
    }

    @Test
    public void shouldReturnErrorWhenInvalidNameInput() throws IOException {
        league.addPlayer(name1, "ThoughtWorkers", 1, 27);
        when(reader.readLine())
                .thenReturn("Sam")
                .thenReturn("1");
        league.findPlayer();
        verify(printStream).println("Cannot Find");
    }



}
